package com.atguigu.guliguli.product.controller;

import com.atguigu.guliguli.product.entity.CategoryEntity;
import com.atguigu.guliguli.product.service.CategoryService;
import com.atguigu.guliguli.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @author: 叶子
 * @date: 2021/11/22 21:38
 **/
@RestController
@RequestMapping("/product/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @RequestMapping("/list/tree")
    public R list(){
        List<CategoryEntity> entities =categoryService.listWithTree();
        return R.ok().put("data",entities);
    }
    @GetMapping("/info/{catId}")
    public R info (@PathVariable("catId")Long catId){
        CategoryEntity category = categoryService.getById(catId);
        return R.ok().put("data",category);
    }
    @RequestMapping("/save")
    public R save(@RequestBody CategoryEntity categoryEntity){
        categoryService.save(categoryEntity);
        return R.ok();
    }

    /**
     * 级联更新
     * @param category
     * @return
     */
    @RequestMapping("/update")
    public R update(@RequestBody CategoryEntity category){
        categoryService.updateCascade(category);
        return R.ok();
    }
    //批量修改
    @RequestMapping("/update/sort")
    public R updateSort (@RequestBody CategoryEntity [] categoryEntities){
        categoryService.updateBatchById(Arrays.asList(categoryEntities));
        return R.ok();
    }
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] catIds){
        categoryService.removeMenuByIds(Arrays.asList(catIds));
        return R.ok();
    }
}